//电子邮件puhalskijsemen@gmail.com
//源码网站 开vpn全局模式打开 http://web3incubators.com/
//电报https://t.me/gamecode999
//网页客服 http://web3incubators.com/kefu.html

import { BundleNames } from "./BundleConfig";
import ResLoader from "./ResLoader";
import DataManager from "./DataManager";
import { LogMgr } from "./LogManager";

/**
 * AudioManager class to manage audio settings and playback.
 * @suggestedName AudioManager
 */
class AudioManager extends DataManager {
    protected _secret_key: string = "audio";
    private _settings: any = null;
    private currentMusic: string | null = null;

    /**
     * 获取musicOn设置
     * @returns {boolean} 如果音乐是开启状态返回true，否则返回false。
     * @suggestedName isMusicOn
     */
    get isMusicOn(): boolean {
        return !this._settings || this._settings.musicOn;
    }

    /**
     * 获取effectOn设置
     * @returns {boolean} 如果效果音是开启状态返回true，否则返回false。
     * @suggestedName isEffectOn
     */
    get isEffectOn(): boolean {
        return !this._settings || this._settings.effectOn;
    }

    /**
     * 获取shakeOn设置
     * @returns {boolean} 如果震动是开启状态返回true，否则返回false。
     * @suggestedName isShakeOn
     */
    get isShakeOn(): boolean {
        return !this._settings || this._settings.shakeOn;
    }

    /**
     * 初始化音频设置。
     * @suggestedName initialize
     */
    initialize(): void {
        const savedSettings = this._load("setting");
        this._settings = {
            music: "",
            musicVolume: 1,
            musicOn: true,
            effectVolume: 1,
            effectOn: true,
            shakeOn: true,
            ...savedSettings
        };
    }

    /**
     * 播放背景音乐
     * @param {string} fileName - 音乐文件名。
     * @param {boolean} [loop=true] - 是否循环播放。
     * @param {string} [bundleName=BundleConfig.BundleNames.Audio] - 资源包名。
     * @suggestedName playBackgroundMusic
     */
    playBackgroundMusic(fileName: string, loop: boolean = true, bundleName: string = BundleNames.Audio): void {
        const musicKey = `${bundleName}&${fileName}`;
        if (musicKey !== this.currentMusic) {
            this.currentMusic = musicKey;
            if (this._settings.musicOn) {
                this._save("setting", this._settings);
                ResLoader.loadRes(bundleName, fileName, cc.AudioClip).then((audioClip: any) => {
                    if (audioClip) {
                        cc.audioEngine.playMusic(audioClip, loop);
                    } else {
                        LogMgr.debug("AudioManager playBgm failed", bundleName, fileName);
                    }
                });
            }
        }
    }

    /**
     * 停止背景音乐
     * @suggestedName stopBackgroundMusic
     */
    stopBackgroundMusic(): void {
        cc.audioEngine.stopMusic();
    }

    /**
     * 切换音乐开关状态
     * @param {boolean} isEnabled - true 时打开音乐，false时关闭音乐。
     * @suggestedName toggleMusic
     */
    toggleMusic(isEnabled: boolean): void {
        this._settings.musicOn = isEnabled;
        if (isEnabled) {
            cc.audioEngine.resumeMusic();
        } else {
            cc.audioEngine.pauseMusic();
        }
        this._save("setting", this._settings);
    }

    /**
     * 设置音乐音量
     * @param {number} volume - 音量级别。
     * @suggestedName setBackgroundMusicVolume
     */
    setBackgroundMusicVolume(volume: number): void {
        this._settings.musicVolume = volume;
        cc.audioEngine.setMusicVolume(volume);
        this._save("setting", this._settings);
    }

    /**
     * 播放音效
     * @param {string} fileName - 音效文件名。
     * @param {boolean} [loop=false] - 是否循环播放。
     * @param {string} [bundleName=BundleConfig.BundleNames.Audio] - 资源包名。
     * @suggestedName playSoundEffect
     */
    playSoundEffect(fileName: string, loop: boolean = false, bundleName: string = BundleNames.Audio): void {
        if (this._settings.effectOn) {
            ResLoader.loadRes(bundleName, fileName, cc.AudioClip).then((audioClip: any) => {
                if (audioClip) {
                    cc.audioEngine.playEffect(audioClip, loop);
                } else {
                    LogMgr.debug("AudioManager playEffect failed", bundleName, fileName);
                }
            });
        }
    }

    /**
     * 切换音效开关状态
     * @param {boolean} isEnabled - true 时打开音效，false时关闭音效。
     * @suggestedName toggleSoundEffects
     */
    toggleSoundEffects(isEnabled: boolean): void {
        this._settings.effectOn = isEnabled;
        if (!isEnabled) {
            cc.audioEngine.stopAllEffects();
        }
        this._save("setting", this._settings);
    }

    /**
     * 设置音效音量
     * @param {number} volume - 音量级别。
     * @suggestedName setSoundEffectVolume
     */
    setSoundEffectVolume(volume: number): void {
        this._settings.effectVolume = volume;
        cc.audioEngine.setEffectsVolume(volume);
        this._save("setting", this._settings);
    }

    /**
     * 切换震动开关状态
     * @param {boolean} isEnabled - true 时打开震动，false时关闭震动。
     * @suggestedName toggleShake
     */
    toggleShake(isEnabled: boolean): void {
        this._settings.shakeOn = isEnabled;
        this._save("setting", this._settings);
    }

    /**
     * 获取当前音频设置
     * @returns {any} 当前设置。
     * @suggestedName getAudioSettings
     */
    getAudioSettings(): any {
        return this._settings;
    }
}

export const AudioMgr = AudioManager.getInstance();